#ifndef PAHOMQTTCLIENT_H
#define PAHOMQTTCLIENT_H

#include "ThmarMqttClient.h"
#include <MQTTAsync.h> // Paho MQTT C++ 库
namespace smq {
/* [未完成]基于 Paho MQTT C 库实现的 MQTT 异步客户端 */
/* 参考：https://eclipse.github.io/paho.mqtt.c/MQTTAsync/html/index.html */
/* 注意：Paho MQTT C++ 库的 API 与 Paho MQTT C 库有些许差异，*/
/* 因此在使用时需要注意 API 的差异。 */
/* 依赖库：eclipse-paho-mqtt-c::paho-mqtt3a-static */
/* 继承自 ThmarMqttClient */
class PahoMqttAsyncClient : public ThmarMqttClient {
public:
    PahoMqttAsyncClient(MQTTAsync_connectOptions conn_opts, 
        const char *serverURI = "tcp://localhost:1883",
        const char *clientID = "ThmarMqttClient", 
        int qos = 2);
    PahoMqttAsyncClient(const char *serverURI = "tcp://localhost:1883", 
        const char *username = NULL, const char *password = NULL, 
        const char *clientID = "ThmarMqttClient", 
        int timeout = 20, int keepAliveInterval = 60, int qos = 2);
    ~PahoMqttAsyncClient();

    int connect() override;
    void disconnect() override;
    int publish(const char* topic, const char* message) override;
    MQTTAsync_connectOptions& getConnectOptions();
    int subscribe(const char* channel) override;
    int unsubscribe(const char* channel) override;
public:
    MQTTAsync client;
    MQTTAsync_connectOptions conn_opts;
    MQTTAsync_disconnectOptions disc_opts;
    void* dispatcherContext;
    ChannelDispatcher channelDispatcher;
    const char *serverURI;
    const char *clientID;
    int qos;
    // 用于记录是否已经初始化
    // 避免重复初始化导致的内存泄漏
    // 因为 PahoMqttAsyncClient 构造函数中会调用 connect() 方法，
    // 所以这里需要一个标志位来记录是否已经初始化
    // 为0表示已经初始化成功，小于0的值表示初始化失败的错误码
    int initialized;
};

} // namespace smq

#endif // PAHOMQTTCLIENT_H